package com.breeze.blockchain.controller.app;

import com.breeze.blockchain.message.DelayJobQueue;
import com.breeze.blockchain.message.RedissonDelayQueueHolder;
import com.breeze.blockchain.message.test.TestDelayMessage;
import com.breeze.blockchain.mybatis.entity.SettingDo;
import com.breeze.blockchain.service.SettingService;
import com.breeze.components.web.core.ResponseDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Optional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * app公开接口
 *
 * @author breeze
 * @since 2024-05-11 23:21:51
 */
@RestController
@Tag(name = "app公开接口")
@RequestMapping("api/app/public")
public class AppPublicController {

  @Resource SettingService settingService;
  @Resource RedissonDelayQueueHolder redissonDelayQueueHolder;

  @Operation(summary = "配置获取")
  @GetMapping("get")
  public ResponseDTO<String> getSetting(@RequestParam String key) {
    return ResponseDTO.ok(
        Optional.ofNullable(settingService.getKeyByValue(key))
            .map(SettingDo::getValue)
            .orElse(null));
  }

  @Operation(summary = "测试延迟队列")
  @GetMapping("addDelayQueue")
  public ResponseDTO<String> addDelayQueue(@RequestParam Long id, @RequestParam Long plusSeconds) {
    redissonDelayQueueHolder.addDelayJob(
        new TestDelayMessage(id),
        LocalDateTime.now().plusSeconds(plusSeconds),
        DelayJobQueue.TEST_QUEUE);
    return ResponseDTO.ok(">>>>TEST加入延迟队列成功");
  }
}
